﻿Ext.ns('Ext.ux.tree');

Ext.ux.tree.TreeLoader = Ext.extend(Ext.tree.TreeLoader, {
        processResponse : function(response, node, callback){
            var json = response.responseText;
            try {
//                if(this.jsonObject != null && typeof(this.jsonObject) != "undefined")
//                    return;
                var o = eval("("+json+")");
                this.jsonObject = o;
                node.beginUpdate();
                for(var i = 0, len = o.length; i < len; i++){
                    var n = this.createNode(o[i]);
                    if(n){
                        node.appendChild(n);
                    }
                }
                node.endUpdate();
                if(typeof callback == "function"){
                    callback(this, node);
                }
            }catch(e){
                this.handleFailure(response);
            }
        },
        nodePath:[],
        getPath:function (obj, id){
            var i=0;
            for(;i<obj.length;i++){
                if(id==obj[i].id)
                    break;
                if(typeof(obj[i].children) !="undefined" && obj[i].children.length > 0){            
                    var childResult = this.getPath(obj[i].children,id);
                    if(childResult)
                        break;
                }else{
                    continue;
                }
            }
            
            if(i<obj.length)
            {
                this.nodePath.unshift(obj[i].id);
                return true;
            }
            
            return false;
        },
        getNodePath: function(nodeID){
            this.nodePath = new Array();
            var result = this.getPath(this.jsonObject, nodeID);
            return this.nodePath;
        }
        
});
Ext.ux.tree.TreePanel = Ext.extend(Ext.tree.TreePanel, {
    
    selectNodeById : function(nodeId){
        var path = this.loader.getNodePath(nodeId);
        if(path.length==0)
        {
          return false;
        }
        for(var i=0;i<path.length-1;i++){
            this.getNodeById(path[i]).expand();
        }
        this.getNodeById(path[i]).fireEvent("click",this.getNodeById(path[i]));
        return true;
    },
    selectNodeByName : function(name){
        var allTradeInfo=tradeTree.loader.jsonObject;   
        var tradeid="";     
        for(i=0;i<allTradeInfo.length;i++)
        {
          tradeid=findtradename(allTradeInfo[i],tradeid,name);
          
   
          if(tradeid!="")
          {
            break;
          }
        }
        return tradeid;        
    }
});
///循环查找树中名称包含所要查找的内容
///obj是节点对象
///tradeid是查询出来的节点的对应ID
///name所需要查询内容
function findtradename(obj,tradeid,name)
{
         if(obj.text!=undefined && obj.text.indexOf(name)!=-1)
          {            
              if(tradeid!="") 
              {
                 tradeid=tradeid+",";
              }
             tradeid=tradeid+obj.id;
             return tradeid;
          }else{
             if(typeof(obj.children) !="undefined" && obj.children.length > 0)
             {
               for(j=0;j<obj.children.length;j++)
               {
                 tradeid=findtradename(obj.children[j],tradeid,name);               
               }
             }
          }
     return tradeid;
}
